<?php

namespace App\Admin\Controllers;

use App\Http\Traits\GlobalConstant;
use App\Models\Category;
use App\Models\Course;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class CourseController extends BaseController
{
    const   INVITE_STATUS = [
        'on'  => ['value' => 0, 'text' => '已下架', 'color' => 'danger'],
        'off' => ['value' => 1, 'text' => '已上架', 'color' => 'default'],
    ];

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('课程列表')
            ->description(' ')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('课程详情')
            ->description(' ')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑课程')
            ->description(' ')
            ->body($this->form($id)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('新增课程')
            ->description('新增课程')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Course());
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            $filter->column(1 / 3, function ($filter) {
                $filter->like('name', '课程名称');

            });
            $filter->column(1 / 3, function ($filter) {
                $filter->equal('valid', '上架状态')->select(['已下架', '已上架']);
            });
            $filter->column(1 / 3, function ($filter) {
            });
        });
//
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', '商品ID')->sortable();
        $grid->column('name', '名称')->limit(20);
        $grid->column('discover', '封面')->lightbox(['width' => 60, 'height' => 60]);
        $grid->column('video_url', '视频')->copyable();
//        $grid->column('tag', '标签');
        $grid->column('price', '价格');
        $grid->column('rebate', '折扣价');
        $grid->column('total', '库存')->display(function ($v) {
            if ($v <= 5) {
                return '<span class="label label-danger">' . $v . '</span>';
            }
            return $v;
        });
        $grid->column('hot', '热度');
        $grid->column('valid', __('状态'))->switch(self::INVITE_STATUS);
        $grid->column('created_at', __('created_at'));
        $grid->column('updated_at', __('updated_at'));

        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $actions->disableView();

        });
        $grid->tools(function ($tools) {
            $tools->batch(function ($batch) {
                $batch->disableDelete();
            });
        });
        $grid->expandFilter();
        Admin::style('.img-thumbnail {width: 60px;height:60px}');
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Course::findOrFail($id));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });
        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($id = 0)
    {
        $form = new Form(new Course());
        $form->text('name', '名称')->required();
        $form->image('discover','封面')->required()->uniqueName();
        $form->currency('price', '价格')->symbol('￥')->required();
        $form->number('total', '库存')->required()->min(0)->value(0);
        $form->number('hot', '热度')->default(0);
        $form->text('desc', '描述');
        $form->text('remark', '注意事项');
        $form->text('tag', '标签')->required();


        $form->tools(function (Form\Tools $tools) {
            // 去掉`删除`按钮
            $tools->disableDelete();
            $tools->disableView();
        });

        $form->footer(function ($footer) {
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
            $footer->disableReset();
        });
        return $form;
    }

}
